﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace Enterprise.Caching
{
    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "This class is instantiated dynamically at runtime by a factory.")]
    internal sealed  class CacheManager : ICacheManager
    {
        
        // Methods
        public IDictionaryEnumerator GetEnumerator()
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Cache.GetEnumerator();
            }
            return null;
        }

        public void Insert(string key, object value)
        {
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Cache.Insert(key, value);
            }
        }

        public void Insert(string key, object value, string[] dependencyKeys)
        {
            if (HttpContext.Current != null)
            {
                CacheDependency dependencies = new CacheDependency(null, dependencyKeys);
                HttpContext.Current.Cache.Insert(key, value, dependencies);
            }
        }

        public void Insert(string key, object value, string[] dependencyKeys, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            if (HttpContext.Current != null)
            {
                CacheDependency dependencies = new CacheDependency(null, dependencyKeys);
                HttpContext.Current.Cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration);
            }
        }

        public object Remove(string key)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Cache.Remove(key);
            }
            return null;
        }

        public void RemoveAll()
        {
            if (HttpContext.Current != null)
            {
                foreach (DictionaryEntry entry in HttpContext.Current.Cache)
                {
                    HttpContext.Current.Cache.Remove(entry.Key as string);
                }
            }
        }

        // Properties
        public int Count
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Cache.Count;
                }
                return -2147483648;
            }
        }

        public object this[string name]
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Cache[name];
                }
                return null;
            }
            set
            {
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Cache[name] = value;
                }
            }
        }

        
    }


}
